﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;

namespace Kiiro.SiteDefinition.ControlTemplates
{
    public partial class AttachmentControl : System.Web.UI.UserControl
    {
        public PageMode ControlMode
        {
            get { return ViewState [ "ControlMode" ] == null ? PageMode.Edit : ( PageMode ) ViewState [ "ControlMode" ]; }
            set { ViewState [ "ControlMode" ] = value; }
        }

        public string Title
        {
            get { return litTitle.Text; }
            set { litTitle.Text = value.Trim ( ); }
        }

        public string ValidationMessage
        {
            get { return validation.Text; }
            set { validation.Text = value; }
        }

        public bool Disabled { get; set; }

        public int MaxLength { get; set; }

        public string FileFormat { get; set; }

        public string ImageClass { get; set; }

        

        protected override void OnPreRender ( EventArgs e )
        {
            if ( ControlMode == PageMode.Display )
            {
                attachments_file_input.Attributes.CssStyle.Add ( "display", "none" );
                //Set the input to false when the page is DiscussionDisplay to make sure that links are created 
                // correctly using the Jquery MultiFile
                if ( Request.Url.ToString().Contains(Constants.ProjectFileUrls.DiscussionDisplay) )
                   attachments_file_input.Visible = false;
            }

            if ( string.IsNullOrEmpty ( Title ) )
            {
                dtTitle.Visible = false;
                dlAttachments.Attributes.CssStyle.Add ( "margin-top", "8px" );
            }

            if ( Disabled )
            {
                litDisabled.Text = "disabled=\"disabled\"";
            }
            // if MaxLength is a default(int) then we are allowing multiple files
            if (MaxLength != default(int))
            {
                if (string.IsNullOrEmpty(FileFormat))
                {
                    addedAttributes.Text = "maxlength=\"1\"";
                }
                else
                {
                    addedAttributes.Text = "maxlength=\"1\" accept=\"" + FileFormat.Trim() + "\"";
                }
            }
            base.OnPreRender ( e );
        }

        internal List<Attachment> GetAttachments ( )
        {
            List<Attachment> attachments = new List<Attachment> ( );
            Attachment attachment = null;
            char[] splitChars = new char [ ] { '|' };

            if ( ControlMode == PageMode.New || ControlMode == PageMode.Edit )
            {
                // deleted
                string [ ] deletedFileNames = txtDeletedAttachments.Text.Split ( splitChars, StringSplitOptions.RemoveEmptyEntries );
                foreach ( string fileName in deletedFileNames )
                {
                    attachment = new Attachment ( );
                    attachment.FileName = fileName;
                    attachment.EditType = EditType.Deleted;
                    attachments.Add ( attachment );
                }

                // created
                for ( int i = 0; i < Page.Request.Files.Count; i++ )
                {
                    HttpPostedFile file = Page.Request.Files [ i ];
                    if ( file != null && file.ContentLength > 0 )
                    {
                        attachment = new Attachment ( );
                        attachment.Bytes = new byte [ file.ContentLength ];
                        attachment.FileName = file.FileName.Substring ( file.FileName.LastIndexOf ( "\\" ) + 1 );
                        file.InputStream.Read ( attachment.Bytes, 0, file.ContentLength );
                        attachment.EditType = EditType.Created;
                        attachments.Add ( attachment );
                    }
                }
            }

            return attachments;
        }

        internal void SetAttachments ( List<Attachment> files )
        {
            if ( files != null )
            {
                foreach ( Attachment attachment in files )
                {
                    HtmlGenericControl li = new HtmlGenericControl( "li" );
                    li.InnerText = attachment.FileName;
                    li.Attributes.Add ( "title", attachment.ServerRelativeUrl );

                    if ( ControlMode == PageMode.Display )
                    {
                        li.Attributes.Add ( "readonly", "readonly" );
                    }

                    if ( Disabled )
                    {
                        li.Attributes.Add ( "disabled", "disabled" );
                    }

                    // this needs to occur to display current user image on editUser page and for the site logo on the editSetup page.
                    if ( Title == "Picture" || Title == "Site Logo" )
                    {
                        li.Attributes.Add ( "isProfilePicture", "isProfilePicture" );
                        li.Attributes.Add ( "imageClass", ImageClass );
                    }

                    attachments.Controls.Add ( li );
                }
            }
        }

        public List<Attachment> ValidateFileExtension(List<Attachment> files)
        {
            #region Blocked Extensions
            Hashtable blockedFiles = new Hashtable();
            blockedFiles.Add(".ade", "");
            blockedFiles.Add(".adp", "");
            blockedFiles.Add(".app", "");
            blockedFiles.Add(".asa", "");
            blockedFiles.Add(".ashx", "");
            blockedFiles.Add(".asmx", "");
            blockedFiles.Add(".asp", "");
            blockedFiles.Add(".bas", "");
            blockedFiles.Add(".bat", "");
            blockedFiles.Add(".cdx", "");
            blockedFiles.Add(".cer", "");
            blockedFiles.Add(".chm", "");
            blockedFiles.Add(".class", "");
            blockedFiles.Add(".cmd", "");
            blockedFiles.Add(".com", "");
            blockedFiles.Add(".config", "");
            blockedFiles.Add(".cpl", "");
            blockedFiles.Add(".crt", "");
            blockedFiles.Add(".csh", "");
            blockedFiles.Add(".dll", "");
            blockedFiles.Add(".exe", "");
            blockedFiles.Add(".fxp", "");
            blockedFiles.Add(".hlp", "");
            blockedFiles.Add(".hta", "");
            blockedFiles.Add(".htr", "");
            blockedFiles.Add(".htw", "");
            blockedFiles.Add(".ida", "");
            blockedFiles.Add(".idc", "");
            blockedFiles.Add(".idq", "");
            blockedFiles.Add(".ins", "");
            blockedFiles.Add(".isp", "");
            blockedFiles.Add(".its", "");
            blockedFiles.Add(".jse", "");
            blockedFiles.Add(".ksh", "");
            blockedFiles.Add(".lnk", "");
            blockedFiles.Add(".mad", "");
            blockedFiles.Add(".maf", "");
            blockedFiles.Add(".mag", "");
            blockedFiles.Add(".mam", "");
            blockedFiles.Add(".maq", "");
            blockedFiles.Add(".mar", "");
            blockedFiles.Add(".mas", "");
            blockedFiles.Add(".mat", "");
            blockedFiles.Add(".mau", "");
            blockedFiles.Add(".mav", "");
            blockedFiles.Add(".maw", "");
            blockedFiles.Add(".mda", "");
            blockedFiles.Add(".mdb", "");
            blockedFiles.Add(".mde", "");
            blockedFiles.Add(".mdt", "");
            blockedFiles.Add(".mdw", "");
            blockedFiles.Add(".mdz", "");
            blockedFiles.Add(".msc", "");
            blockedFiles.Add(".msh", "");
            blockedFiles.Add(".msh1", "");
            blockedFiles.Add(".msh1xml", "");
            blockedFiles.Add(".msh2", "");
            blockedFiles.Add(".msh2xml", "");
            blockedFiles.Add(".mshxml", "");
            blockedFiles.Add(".msi", "");
            blockedFiles.Add(".msp", "");
            blockedFiles.Add(".mst", "");
            blockedFiles.Add(".ops", "");
            blockedFiles.Add(".pcd", "");
            blockedFiles.Add(".pif", "");
            blockedFiles.Add(".prf", "");
            blockedFiles.Add(".prg", "");
            blockedFiles.Add(".printer", "");
            blockedFiles.Add(".pst", "");
            blockedFiles.Add(".reg", "");
            blockedFiles.Add(".rem", "");
            blockedFiles.Add(".scf", "");
            blockedFiles.Add(".scr", "");
            blockedFiles.Add(".sct", "");
            blockedFiles.Add(".shb", "");
            blockedFiles.Add(".shs", "");
            blockedFiles.Add(".shtm", "");
            blockedFiles.Add(".shtml", "");
            blockedFiles.Add(".soap", "");
            blockedFiles.Add(".stm", "");
            blockedFiles.Add(".url", "");
            blockedFiles.Add(".vb", "");
            blockedFiles.Add(".vbe", "");
            blockedFiles.Add(".vbs", "");
            blockedFiles.Add(".ws", "");
            blockedFiles.Add(".wsc", "");
            blockedFiles.Add(".wsf", "");
            blockedFiles.Add(".wsh", "");
            #endregion

            foreach (Attachment file in files)
            {
                if (file.FileName.LastIndexOf('.') > 0)
                {
                    string extension = file.FileName.Substring(file.FileName.LastIndexOf('.'), file.FileName.Length - file.FileName.LastIndexOf('.'));
                    if (blockedFiles.Contains(extension))
                    {
                        file.EditType = EditType.Unchanged;
                    }
                }
            }
            return files;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/CONTROLTEMPLATES/Kiiro/AttachmentControl.ascx.cs $
 * 
 * 9     12/14/09 2:10p Shafia
 * attachments file extensions validation for discussions, tasks, and
 * issues
 * 
 * 8     7/11/09 2:41p Camerons
 * 
 * 7     23/10/09 10:23a Shafia
 * 
 * 6     16/10/09 5:41p Shafia
 * Added MaxLength to track the limit of files allowed to upload and
 * FileFormat to limit file types
 * 
 * 5     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/